package com.ygqh.baby.service.impl;

import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.AdminPurchaseDetail;
import com.ygqh.baby.mapper.AdminPurchaseDetailMapper;
import com.ygqh.baby.model.AdminPurchaseDetailInCount;
import com.ygqh.baby.service.AdminPurchaseDetailService;

import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.AdminPurchaseDetailExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class AdminPurchaseDetailServiceImpl implements AdminPurchaseDetailService {

	@Autowired
	private AdminPurchaseDetailMapper adminPurchaseDetailMapper;

	@Override
	public List<AdminPurchaseDetail> find() {
		AdminPurchaseDetailExample example = new AdminPurchaseDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminPurchaseDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminPurchaseDetailExample example = new AdminPurchaseDetailExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminPurchaseDetailMapper.countByExample(example);
	}

	@Override
	public AdminPurchaseDetail findById(Long id) {
		return adminPurchaseDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(AdminPurchaseDetail adminPurchaseDetail) {
		return adminPurchaseDetailMapper.insertSelective(adminPurchaseDetail);
	}

	@Override
	public int update(AdminPurchaseDetail adminPurchaseDetail) {
		return adminPurchaseDetailMapper.updateByPrimaryKeySelective(adminPurchaseDetail);
	}

	@Override
	public ResultSet<AdminPurchaseDetail> search(QueryInfo queryInfo, String q) {
		List<AdminPurchaseDetail> list = adminPurchaseDetailMapper.selectSuper(queryInfo, q);
		int count = adminPurchaseDetailMapper.countSuper(q);
		ResultSet<AdminPurchaseDetail> resultSet = new ResultSet<AdminPurchaseDetail>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminPurchaseDetailExample example = new AdminPurchaseDetailExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminPurchaseDetail record = new AdminPurchaseDetail();
		record.setStatus(DataStatus.Delete);

		return adminPurchaseDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(AdminPurchaseDetail adminPurchaseDetail) {
		int r = 0;
		if (adminPurchaseDetail.getId() != null) {
			r = this.update(adminPurchaseDetail);
		} else {
			r = this.save(adminPurchaseDetail);
		}
		return r;
	}

	@Override
	public List<AdminPurchaseDetail> findByPurchaseCode(String purchaseCode) {
		AdminPurchaseDetailExample example = new AdminPurchaseDetailExample();
		example.createCriteria().andPurchaseCodeEqualTo(purchaseCode).andStatusEqualTo(DataStatus.Valid.name());
		List<AdminPurchaseDetail> list = adminPurchaseDetailMapper.selectByExample(example);
		return list;
	}

	@Override
	public int updateBatch(List<AdminPurchaseDetail> purchaseDetail) {
		return adminPurchaseDetailMapper.updateStockAmountBatch(purchaseDetail);
	}

	@Override
	public List<AdminPurchaseDetailInCount> findPurchaseDetailInStockCountByContractCode(String contractCode) {
		return adminPurchaseDetailMapper.selectPurchaseDetailInStockCountByContractCode(contractCode);
	}

}
